package com.lineage.server;

import com.lineage.config.Config;
import com.lineage.echo.ServerExecutor;
import com.lineage.server.thread.GeneralThreadPool;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TimerTask;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class EchoServerTimer extends TimerTask {
  private static final Log _log = LogFactory.getLog(EchoServerTimer.class);
  
  private static EchoServerTimer _instance;
  
  private static final Map<Integer, ServerExecutor> _echoList = new HashMap<>();
  
  public static EchoServerTimer get() {
    if (_instance == null)
      _instance = new EchoServerTimer(); 
    return _instance;
  }
  
  public void start() {
    try {
      if (_echoList.isEmpty())
        startEcho(); 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      return;
    } finally {
      if (Config.RESTART_LOGIN > 0) {
        _log.warn("监听端口重置作业启动 间隔时间:" + Config.RESTART_LOGIN + "分钟。");
        int timeMillis = Config.RESTART_LOGIN * 60 * 1000;
        GeneralThreadPool.get().aiScheduleAtFixedRate(this, timeMillis, timeMillis);
      } 
    } 
  }
  
  public void run() {
    try {
      _log.warn("监听端口重置作业!");
      try {
        stopEcho();
        startEcho();
      } catch (Exception e) {
        _log.error("重新启动端口作业失败!!", e);
      } 
    } catch (Exception e) {
      _log.error("监听端口重置作业失败!!", e);
    } finally {
      _log.warn("监听端口重置作业完成!!");
    } 
  }
  
  public void reStart() {
    try {
      if (Shutdown.SHUTDOWN)
        return; 
      if (!_echoList.isEmpty()) {
        stopEcho();
        Thread.sleep(2000L);
        startEcho();
      } else {
        _log.error("监听端口重置作业失败(目前无任何监听线程)!!");
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void startEcho() {
    try {
      String[] portList = Config.GAME_SERVER_PORT.split("-");
      String[] array;
      int length = (array = portList).length;
      int i = 0;
      while (i < length) {
        String ports = array[i];
        int key = Integer.parseInt(ports);
        ServerExecutor echoServer = new ServerExecutor(key);
        if (echoServer != null) {
          _echoList.put(new Integer(key), echoServer);
          echoServer.stsrtEcho();
        } 
        Thread.sleep(100L);
        i++;
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void stopEcho() {
    try {
      if (!_echoList.isEmpty()) {
        Iterator<Integer> iterator = _echoList.keySet().iterator();
        while (iterator.hasNext()) {
          Integer key = iterator.next();
          ServerExecutor echoServer = _echoList.get(key);
          if (echoServer != null)
            echoServer.stopEcho(); 
          Thread.sleep(100L);
        } 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public boolean isPort(int key) {
    try {
      return (_echoList.get(new Integer(key)) != null);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      return false;
    } 
  }
  
  public void stopPort(int key) {
    try {
      ServerExecutor echoServer = _echoList.get(new Integer(key));
      if (echoServer != null) {
        echoServer.stopEcho();
        _echoList.remove(new Integer(key));
      } else {
        _log.warn("关闭指定监听端口 作业失败:该端口未在作用中!");
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void startPort(int key) {
    try {
      ServerExecutor echoServer = _echoList.get(new Integer(key));
      if (echoServer == null) {
        echoServer = new ServerExecutor(key);
        _echoList.put(new Integer(key), echoServer);
        echoServer.stsrtEcho();
      } else {
        _log.warn("启用指定监听端口 作业失败:该端口已在作用中!");
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
}
